package com.mit.service.purchase.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.purchase.dto.MaterialDTO;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.purchase.dao.IMaterialDAO;

public class MaterialDAOImpl extends AbstractDAO implements IMaterialDAO {

	@Override
	public MaterialDTO createMaterial(MaterialDTO to) {
		Integer id = (Integer ) getSqlMapClientTemplate().insert("insertMaterial",to);
		to.setId(id);
		return to;
	}

	@Override
	public boolean hasMaterial(Integer supplier, String code, String name,
			Integer excludedId) {
		HashMap param = new HashMap();
		param.put("supplierId", supplier);
		param.put("name", name);
		param.put("code",code);
		param.put("id", excludedId);
		int rowCount = (Integer) getSqlMapClientTemplate().queryForObject("selectMaterialCountByCodeOrName", param);
		return rowCount > 0;
	}

	@Override
	public List<MaterialDTO> retrieveMaterials(Integer supplier,String activeIndicator) {
		HashMap param = new HashMap();
		param.put("supplierId", supplier);
		param.put("actInd", activeIndicator);

		return (List<MaterialDTO>)  getSqlMapClientTemplate().queryForList("selectMaterialsBySupplier", param);
	}

	@Override
	public MaterialDTO updateMaterial(MaterialDTO to)
			throws NoRecordUpdateException {
		int rowCount = getSqlMapClientTemplate().update("updateMaterial", to);
		if(rowCount < 1)
			throw new NoRecordUpdateException();
		return to;
	}

	@Override
	public MaterialDTO retrieveMaterial(Integer id) {
		
		return (MaterialDTO) getSqlMapClientTemplate().queryForObject("selectMaterialById", id);
	}

}
